package utils

import (
	"fmt"
	"os"
	"strings"

	"github.com/spf13/viper"
)

type CertBaseInfo struct {
	Domain string `json:"domain" mapstructure:"domain" yaml:"domain"`
	Path   string `json:"path" mapstructure:"path" yaml:"path"`
}
type CertInfoList struct {
	CertsInfo []*CertBaseInfo `json:"certs_info" yaml:"certs_info" mapstructure:"certs_info"`
}

func PaserFile(path string) *CertInfoList {
	ss := strings.Split(path, "/")
	if ss[0] != "" {
		dir, err := os.Getwd()
		if err != nil {
			Log.Errorf("获取当前目录失败,错误信息: %s", err)
		} else {
			path = dir + "/" + path
		}
	}
	fmt.Println(path)
	viper.SetConfigType("yaml")
	viper.SetConfigFile(path)

	err := viper.ReadInConfig()
	if err != nil {
		Log.Fatalf("读取配置文件失败,错误信息: %s", err)
	}
	var _ci = new(CertInfoList)
	err = viper.Unmarshal(_ci)
	if err != nil {
		Log.Fatalf("解析配置文件失败,错误信息: %s", err)
	}
	return _ci
}
